﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Sandbox.Scene
{
	public abstract class MapSceneObject
	{
		public MapSceneObject()
		{
			IsVisible = true;
			IsRendered = false;
		}

		public void OnAttached(MapScene scene)
		{
			Scene = scene;

			if (IsVisible && scene.IsRendering && !IsRendered)
				StartRendering();
		}

		public void OnDetached(MapScene scene)
		{
			if (IsRendered && Scene.IsRendering)
				StopRendering();

			Scene = null;
		}

		public void Show()
		{
			IsVisible = true;
			if (!IsRendered && Scene.IsRendering)
				StartRendering();
		}

		public void Hide()
		{
			IsVisible = false;
			if (IsRendered && Scene.IsRendering)
				StopRendering();
		}

		public void Reappear()
		{
			if (IsVisible && Scene.IsRendering)
			{
				StartRendering();
			}
		}

		public bool IsVisible
		{
			get;
			private set;
		}

		protected bool IsRendered
		{
			get;
			private set;
		}

		private void StartRendering()
		{
			IsRendered = true;
			Display();
		}

		private void StopRendering()
		{
			IsRendered = false;
			Disappear();
		}

		protected abstract void Display();
		protected abstract void Disappear();

		protected MapScene Scene
		{
			get;
			private set;
		}

		protected virtual Canvas SceneCanvas
		{
			get { return Scene.GetSceneObjectCanvas(MapSceneObjectLayers.AboveOverlay); }
		}
	}
}
